/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ws.converter;

import entidade.persistencia.Usuario;
import java.net.URI;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import entidade.persistencia.TipoUsuario;
import entidade.persistencia.Funcionario;
import entidade.persistencia.Manutencao;
import entidade.persistencia.Empresa;
import entidade.persistencia.Venda;
import java.util.Collection;
import entidade.persistencia.Compra;
import java.util.List;

/**
 *
 * @author victorinox
 */

@XmlRootElement(name = "usuario")
public class UsuarioConverter {
    private Usuario entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of UsuarioConverter */
    public UsuarioConverter() {
        entity = new Usuario();
    }

    /**
     * Creates a new instance of UsuarioConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public UsuarioConverter(Usuario entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getUsuarioId() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getManutencaoCollection();
        getCompraCollection();
        getEmpresaId();
        getTipoUsuarioId();
        getFuncionarioCollection();
        getVendaCollection();
        getVendaCollection1();
    }

    /**
     * Creates a new instance of UsuarioConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public UsuarioConverter(Usuario entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for usuarioId.
     *
     * @return value for usuarioId
     */
    @XmlElement
    public Integer getUsuarioId() {
        return (expandLevel > 0) ? entity.getUsuarioId() : null;
    }

    /**
     * Setter for usuarioId.
     *
     * @param value the value to set
     */
    public void setUsuarioId(Integer value) {
        entity.setUsuarioId(value);
    }

    /**
     * Getter for nome.
     *
     * @return value for nome
     */
    @XmlElement
    public String getNome() {
        return (expandLevel > 0) ? entity.getNome() : null;
    }

    /**
     * Setter for nome.
     *
     * @param value the value to set
     */
    public void setNome(String value) {
        entity.setNome(value);
    }

    /**
     * Getter for email.
     *
     * @return value for email
     */
    @XmlElement
    public String getEmail() {
        return (expandLevel > 0) ? entity.getEmail() : null;
    }

    /**
     * Setter for email.
     *
     * @param value the value to set
     */
    public void setEmail(String value) {
        entity.setEmail(value);
    }

    /**
     * Getter for senha.
     *
     * @return value for senha
     */
    @XmlElement
    public String getSenha() {
        return (expandLevel > 0) ? entity.getSenha() : null;
    }

    /**
     * Setter for senha.
     *
     * @param value the value to set
     */
    public void setSenha(String value) {
        entity.setSenha(value);
    }

    /**
     * Getter for dataUltimoLogin.
     *
     * @return value for dataUltimoLogin
     */
    @XmlElement
    public Date getDataUltimoLogin() {
        return (expandLevel > 0) ? entity.getDataUltimoLogin() : null;
    }

    /**
     * Setter for dataUltimoLogin.
     *
     * @param value the value to set
     */
    public void setDataUltimoLogin(Date value) {
        entity.setDataUltimoLogin(value);
    }

    /**
     * Getter for dddi1.
     *
     * @return value for dddi1
     */
    @XmlElement
    public String getDddi1() {
        return (expandLevel > 0) ? entity.getDddi1() : null;
    }

    /**
     * Setter for dddi1.
     *
     * @param value the value to set
     */
    public void setDddi1(String value) {
        entity.setDddi1(value);
    }

    /**
     * Getter for ddd1.
     *
     * @return value for ddd1
     */
    @XmlElement
    public String getDdd1() {
        return (expandLevel > 0) ? entity.getDdd1() : null;
    }

    /**
     * Setter for ddd1.
     *
     * @param value the value to set
     */
    public void setDdd1(String value) {
        entity.setDdd1(value);
    }

    /**
     * Getter for numero1.
     *
     * @return value for numero1
     */
    @XmlElement
    public Integer getNumero1() {
        return (expandLevel > 0) ? entity.getNumero1() : null;
    }

    /**
     * Setter for numero1.
     *
     * @param value the value to set
     */
    public void setNumero1(Integer value) {
        entity.setNumero1(value);
    }

    /**
     * Getter for ddi2.
     *
     * @return value for ddi2
     */
    @XmlElement
    public Integer getDdi2() {
        return (expandLevel > 0) ? entity.getDdi2() : null;
    }

    /**
     * Setter for ddi2.
     *
     * @param value the value to set
     */
    public void setDdi2(Integer value) {
        entity.setDdi2(value);
    }

    /**
     * Getter for ddd2.
     *
     * @return value for ddd2
     */
    @XmlElement
    public Integer getDdd2() {
        return (expandLevel > 0) ? entity.getDdd2() : null;
    }

    /**
     * Setter for ddd2.
     *
     * @param value the value to set
     */
    public void setDdd2(Integer value) {
        entity.setDdd2(value);
    }

    /**
     * Getter for numero2.
     *
     * @return value for numero2
     */
    @XmlElement
    public Integer getNumero2() {
        return (expandLevel > 0) ? entity.getNumero2() : null;
    }

    /**
     * Setter for numero2.
     *
     * @param value the value to set
     */
    public void setNumero2(Integer value) {
        entity.setNumero2(value);
    }

    /**
     * Getter for ddi3.
     *
     * @return value for ddi3
     */
    @XmlElement
    public Integer getDdi3() {
        return (expandLevel > 0) ? entity.getDdi3() : null;
    }

    /**
     * Setter for ddi3.
     *
     * @param value the value to set
     */
    public void setDdi3(Integer value) {
        entity.setDdi3(value);
    }

    /**
     * Getter for ddd3.
     *
     * @return value for ddd3
     */
    @XmlElement
    public Integer getDdd3() {
        return (expandLevel > 0) ? entity.getDdd3() : null;
    }

    /**
     * Setter for ddd3.
     *
     * @param value the value to set
     */
    public void setDdd3(Integer value) {
        entity.setDdd3(value);
    }

    /**
     * Getter for numero3.
     *
     * @return value for numero3
     */
    @XmlElement
    public Integer getNumero3() {
        return (expandLevel > 0) ? entity.getNumero3() : null;
    }

    /**
     * Setter for numero3.
     *
     * @param value the value to set
     */
    public void setNumero3(Integer value) {
        entity.setNumero3(value);
    }

    /**
     * Getter for dataUltimaAlteracao.
     *
     * @return value for dataUltimaAlteracao
     */
    @XmlElement
    public Date getDataUltimaAlteracao() {
        return (expandLevel > 0) ? entity.getDataUltimaAlteracao() : null;
    }

    /**
     * Setter for dataUltimaAlteracao.
     *
     * @param value the value to set
     */
    public void setDataUltimaAlteracao(Date value) {
        entity.setDataUltimaAlteracao(value);
    }

    /**
     * Getter for manutencaoCollection.
     *
     * @return value for manutencaoCollection
     */
    @XmlElement
    public ManutencaoesConverter getManutencaoCollection() {
        if (expandLevel > 0) {
            if (entity.getManutencaoCollection() != null) {
                return new ManutencaoesConverter(entity.getManutencaoCollection(), uri.resolve("manutencaoCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for manutencaoCollection.
     *
     * @param value the value to set
     */
    public void setManutencaoCollection(ManutencaoesConverter value) {
        entity.setManutencaoCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for compraCollection.
     *
     * @return value for compraCollection
     */
    @XmlElement
    public ComprasConverter getCompraCollection() {
        if (expandLevel > 0) {
            if (entity.getCompraCollection() != null) {
                return new ComprasConverter(entity.getCompraCollection(), uri.resolve("compraCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for compraCollection.
     *
     * @param value the value to set
     */
    public void setCompraCollection(ComprasConverter value) {
        entity.setCompraCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for empresaId.
     *
     * @return value for empresaId
     */
    @XmlElement
    public EmpresaConverter getEmpresaId() {
        if (expandLevel > 0) {
            if (entity.getEmpresaId() != null) {
                return new EmpresaConverter(entity.getEmpresaId(), uri.resolve("empresaId/"), expandLevel - 1, false);
            }
        }
        return null;
    }

    /**
     * Setter for empresaId.
     *
     * @param value the value to set
     */
    public void setEmpresaId(EmpresaConverter value) {
        entity.setEmpresaId((value != null) ? value.getEntity() : null);
    }

    /**
     * Getter for tipoUsuarioId.
     *
     * @return value for tipoUsuarioId
     */
    @XmlElement
    public TipoUsuarioConverter getTipoUsuarioId() {
        if (expandLevel > 0) {
            if (entity.getTipoUsuarioId() != null) {
                return new TipoUsuarioConverter(entity.getTipoUsuarioId(), uri.resolve("tipoUsuarioId/"), expandLevel - 1, false);
            }
        }
        return null;
    }

    /**
     * Setter for tipoUsuarioId.
     *
     * @param value the value to set
     */
    public void setTipoUsuarioId(TipoUsuarioConverter value) {
        entity.setTipoUsuarioId((value != null) ? value.getEntity() : null);
    }

    /**
     * Getter for funcionarioCollection.
     *
     * @return value for funcionarioCollection
     */
    @XmlElement
    public FuncionarioesConverter getFuncionarioCollection() {
        if (expandLevel > 0) {
            if (entity.getFuncionarioCollection() != null) {
                return new FuncionarioesConverter(entity.getFuncionarioCollection(), uri.resolve("funcionarioCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for funcionarioCollection.
     *
     * @param value the value to set
     */
    public void setFuncionarioCollection(FuncionarioesConverter value) {
        entity.setFuncionarioCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for vendaCollection.
     *
     * @return value for vendaCollection
     */
    @XmlElement
    public VendasConverter getVendaCollection() {
        if (expandLevel > 0) {
            if (entity.getVendaCollection() != null) {
                return new VendasConverter(entity.getVendaCollection(), uri.resolve("vendaCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for vendaCollection.
     *
     * @param value the value to set
     */
    public void setVendaCollection(VendasConverter value) {
        entity.setVendaCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for vendaCollection1.
     *
     * @return value for vendaCollection1
     */
    @XmlElement
    public VendasConverter getVendaCollection1() {
        if (expandLevel > 0) {
            if (entity.getVendaCollection1() != null) {
                return new VendasConverter(entity.getVendaCollection1(), uri.resolve("vendaCollection1/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for vendaCollection1.
     *
     * @param value the value to set
     */
    public void setVendaCollection1(VendasConverter value) {
        entity.setVendaCollection1((value != null) ? value.getEntities() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the Usuario entity.
     *
     * @return an entity
     */
    @XmlTransient
    public Usuario getEntity() {
        if (entity.getUsuarioId() == null) {
            UsuarioConverter converter = UriResolver.getInstance().resolve(UsuarioConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved Usuario entity.
     *
     * @return an resolved entity
     */
    public Usuario resolveEntity(EntityManager em) {
        List<Manutencao> manutencaoCollection = entity.getManutencaoCollection();
        List<Manutencao> newmanutencaoCollection = new java.util.ArrayList<Manutencao>();
        for (Manutencao item : manutencaoCollection) {
            newmanutencaoCollection.add(em.getReference(Manutencao.class, item.getManutencaoId()));
        }
        entity.setManutencaoCollection(newmanutencaoCollection);
        List<Compra> compraCollection = entity.getCompraCollection();
        List<Compra> newcompraCollection = new java.util.ArrayList<Compra>();
        for (Compra item : compraCollection) {
            newcompraCollection.add(em.getReference(Compra.class, item.getCompraId()));
        }
        entity.setCompraCollection(newcompraCollection);
        Empresa empresaId = entity.getEmpresaId();
        if (empresaId != null) {
            entity.setEmpresaId(em.getReference(Empresa.class, empresaId.getEmpresaId()));
        }
        TipoUsuario tipoUsuarioId = entity.getTipoUsuarioId();
        if (tipoUsuarioId != null) {
            entity.setTipoUsuarioId(em.getReference(TipoUsuario.class, tipoUsuarioId.getTipoUsuarioId()));
        }
        List<Funcionario> funcionarioCollection = entity.getFuncionarioCollection();
        List<Funcionario> newfuncionarioCollection = new java.util.ArrayList<Funcionario>();
        for (Funcionario item : funcionarioCollection) {
            newfuncionarioCollection.add(em.getReference(Funcionario.class, item.getFuncionarioId()));
        }
        entity.setFuncionarioCollection(newfuncionarioCollection);
        List<Venda> vendaCollection = entity.getVendaCollection();
        List<Venda> newvendaCollection = new java.util.ArrayList<Venda>();
        for (Venda item : vendaCollection) {
            newvendaCollection.add(em.getReference(Venda.class, item.getVendaId()));
        }
        entity.setVendaCollection(newvendaCollection);
        List<Venda> vendaCollection1 = entity.getVendaCollection1();
        List<Venda> newvendaCollection1 = new java.util.ArrayList<Venda>();
        for (Venda item : vendaCollection1) {
            newvendaCollection1.add(em.getReference(Venda.class, item.getVendaId()));
        }
        entity.setVendaCollection1(newvendaCollection1);
        return entity;
    }
}
